﻿using System;
using System.Diagnostics.Contracts;
using System.IO;

namespace OpenLS.Spreadsheet.Formats.Biff.Records
{
    internal class BookExtRecord : BiffRecord
    {
        private FrtHeader
            frtHeader;

        private uint cb;
        private byte flag1;
        private byte flag2;
        private ushort reserved;
        private byte? grbit1;
        private byte? grbit2;

        protected override void ReadContent(BiffReaderContext context, int length)
        {
            frtHeader.Read(context.Reader);
            Contract.Assert(frtHeader._rt == 0x0863);
            cb = context.ReadUInt32();
            flag1 = context.ReadByte();
            flag2 = context.ReadByte();
            reserved = context.ReadUInt16();
            if (cb > 20)
            grbit1 = context.ReadByte();
            if (cb > 21)
            grbit2 = context.ReadByte();
        }

        internal override BiffRecordType RecordType
        {
            get { return BiffRecordType.BookExt; }
        }

        internal override void WriteContent(BiffWriterWorkbookContext context)
        {
            this.frtHeader.Write(context);
            context.Writer.Write(cb);
            context.Writer.Write(flag1);
            context.Writer.Write(flag2);
            context.Writer.Write(reserved);
            if (grbit1 != null)
                context.Writer.Write(grbit1.Value);
            if (grbit2 != null)
                context.Writer.Write(grbit2.Value);

        }
    }
}